package TouristManagementBO.Site;


import org.omg.CosNaming.NamingContext;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import TouristManagement.*;

public class SiteServerThread extends Thread{
	
	short idSite;
	ServerNamingTourist leServerNamingTourist;
	
	public SiteServerThread(short idSite) {
		super();
		this.idSite = idSite;		
	}
	
	public void initServer(){
		this.connectToServerNamingTourist();
	}
	
	public void connectToServerNamingTourist(){
		//init VisitManagement server
		try {
			
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent("ServerNamingTourist","");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantServerNamingTourist = nameRoot.resolve(nameToFind);
	        
	        leServerNamingTourist = ServerNamingTouristHelper.narrow(distantServerNamingTourist);
	        
	        System.out.println("Tourist naming server connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void run() {
		try {					            
	            String args[] = null;
            
		        // Intialisation de l'ORB
		        //************************
		        org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

		        // Gestion du POA
		        //****************
		        // Recuperation du POA
		        POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

		        // Creation du servant
		        //*********************
		        SiteImpl leSite = new SiteImpl(idSite);
		        leSite.initServer();

		        // Activer le servant au sein du POA et recuperer son ID
		        rootPOA.activate_object(leSite);

		        // Activer le POA manager
		        rootPOA.the_POAManager().activate();
		
		        // Enregistrement dans le service de nommage
		        //*******************************************
		        // Recuperation du naming service
		        NamingContext nameRoot=org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

		        // Construction du nom a enregistrer
		        org.omg.CosNaming.NameComponent[] nameToRegister = new org.omg.CosNaming.NameComponent[1];
		        //enregister dans le Naming service
		        nameToRegister[0] = new org.omg.CosNaming.NameComponent(leServerNamingTourist.nomSites()[idSite-1],"");

		        // Enregistrement de l'objet CORBA dans le service de noms
		        nameRoot.rebind(nameToRegister,rootPOA.servant_to_reference(leSite));
		
//		        String IORServant = orb.object_to_string(rootPOA.servant_to_reference(leSite));
		        System.out.println("Site " + leServerNamingTourist.nomSites()[idSite-1] + " created, id "+idSite);

		        // Lancement de l'ORB et mise en attente de requete
		        //**************************************************
		        orb.run();
		    }
			catch (Exception e) {
				e.printStackTrace();
			}
		} // main

}
